package org.erp.spu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.erp.common.page.PageQueryModel;
import org.erp.spu.entity.SpuSeries;
import org.erp.spu.entity.bo.SpuSeriesBo;
import org.erp.spu.entity.dto.SpuSeriesDto;
import org.erp.spu.entity.vo.SpuSeriesVo;
import org.erp.spu.mapper.SpuSeriesMapper;
import org.erp.spu.service.SpuSeriesService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 系列表(SpuSeries)表服务实现类
 *
 * @author dingding
 * @since 2025-11-05 17:45:57
 */
@Service("spuSeriesService")
public class SpuSeriesServiceImpl extends ServiceImpl<SpuSeriesMapper, SpuSeries> implements SpuSeriesService {

    @Resource
    private SpuSeriesMapper spuSeriesMapper;

    @Override
    public List<SpuSeriesVo> getList(SpuSeriesBo spuSeriesBo) {
        LambdaQueryWrapper<SpuSeries> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(spuSeriesBo.getSeriesName()), SpuSeries::getSeriesName, spuSeriesBo.getSeriesName());
        queryWrapper.eq(spuSeriesBo.getBrandId()!=null, SpuSeries::getBrandId, spuSeriesBo.getBrandId());
        queryWrapper.eq(spuSeriesBo.getCategoryId()!=null, SpuSeries::getCategoryId, spuSeriesBo.getCategoryId());
        queryWrapper.eq(SpuSeries::getDelFlag, 0);
        List<SpuSeries> seriesList = spuSeriesMapper.selectList(queryWrapper);
        return seriesList.stream()
                .map(this::convertToVo)
                .collect(Collectors.toList());
    }

    @Override
    public PageQueryModel<SpuSeriesVo> getListByPage(SpuSeriesDto spuSeriesDto) {
        LambdaQueryWrapper<SpuSeries> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(spuSeriesDto.getSeriesName()), SpuSeries::getSeriesName, spuSeriesDto.getSeriesName());
        queryWrapper.eq(spuSeriesDto.getBrandId()!=null, SpuSeries::getBrandId, spuSeriesDto.getBrandId());
        queryWrapper.eq(spuSeriesDto.getCategoryId()!=null, SpuSeries::getCategoryId, spuSeriesDto.getCategoryId());
        queryWrapper.eq(SpuSeries::getDelFlag, 0);
        Page<SpuSeries> page = this.page(spuSeriesDto.buildPage(), queryWrapper);
        List<SpuSeriesVo> voList = page.getRecords().stream()
                .map(this::convertToVo)
                .collect(Collectors.toList());
        return PageQueryModel.of(page.getPages(), voList);
    }

    @Override
    public boolean addSpuSeries(SpuSeriesDto spuSeriesDto) {
        Assert.isTrue(!checkSeriesName(spuSeriesDto), "系列名称已存在");
        SpuSeries spuSeries = new SpuSeries();
        BeanUtils.copyProperties(spuSeriesDto, spuSeries);
        return spuSeriesMapper.insert(spuSeries) > 0;
    }

    @Override
    public boolean updateSpuSeries(SpuSeriesDto spuSeriesDto) {
        Assert.isTrue(!checkSeriesName(spuSeriesDto), "系列名称已存在");
        SpuSeries spuSeries = new SpuSeries();
        BeanUtils.copyProperties(spuSeriesDto, spuSeries);
        return spuSeriesMapper.updateById(spuSeries) > 0;
    }

    @Override
    public SpuSeriesVo getSpuSeriesVoById(Long id) {
        SpuSeries spuSeries = spuSeriesMapper.selectById(id);
        return convertToVo(spuSeries);
    }

    /**
     * 转换为VO对象
     *
     * @param spuSeries
     * @return
     */
    private SpuSeriesVo convertToVo(SpuSeries spuSeries) {
        SpuSeriesVo vo = new SpuSeriesVo();
        BeanUtils.copyProperties(spuSeries, vo);
        return vo;
    }

    /**
     * 检查系列名称是否存在
     *
     * @param spuSeriesDto
     * @return
     */
    private boolean checkSeriesName(SpuSeriesDto spuSeriesDto) {
        LambdaQueryWrapper<SpuSeries> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpuSeries::getSeriesName, spuSeriesDto.getSeriesName());
        queryWrapper.eq(SpuSeries::getDelFlag, 0);
        // 如果是修改操作，排除当前记录
        if (spuSeriesDto.getId() != null) {
            queryWrapper.ne(SpuSeries::getId, spuSeriesDto.getId());
        }
        return this.count(queryWrapper) > 0;
    }
}
